package com.tool.tcpimg;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JPanel;

import com.updateNew.MyIsif;
import org.apache.commons.lang.StringUtils;
import org.come.Frame.FrameMessageFrame;
import org.come.Frame.TestSetupJframe;
import org.come.Frame.ZhuFrame;
import org.come.Jpanel.FrameMessageChangeJpanel;
import org.come.Jpanel.TestSetupJpanel;
import org.come.bean.ImgZoom;
import org.come.control.TestMain;
import org.come.until.CutButtonImage;
import org.come.until.ScrenceUntil;

/**
 * 对话框
 *
 * @author Administrator
 *
 */
public class ChatBox extends JPanel
{
	private static final long serialVersionUID = 1L;
	private List<RichLabel> labels;
	// 显示偏差
	private int deviation = 0;
	// 是否禁言 flase禁言
	private boolean Gag = true;
	// 屏幕透明度
	private float Alpha = 0.0f;
	// 判断显示位置(显示在左 true)
	private boolean display = false;
	// 聊天框的大小
	private int w = 0;
	private int h = 0;
//	private static final long serialVersionUID = 1L;
	//private List<RichLabel> labels;
	// 显示偏差
	public static String vs;
//	private int deviation;
	//private boolean Gag;
//	private float Alpha;
	private float AlphaSS;
	//private boolean display;
	//private int w;
	//private int h;
	public Font font;
	public static ImgZoom imgZoom;
	public static ImgZoom imgZoomS;
	public static ImgZoom imgZoomSx;
	private int maxSize = 1000;
	static boolean flag = true;
	public ChatBox() {
		this.deviation = 0;
		// 是否禁言 flase禁言
		this.Gag = true;
		// 屏幕透明度
		this.Alpha = 0.0f;
		// 判断显示位置(显示在左 true)
		this.AlphaSS = 0.5f;
		this.display = false;
		// 聊天框的大小
		this.w = 0;
		this.h = 0;
		this.labels = new ArrayList();
		this.setBackground(UIUtils.Color_BACK);
	}
	public ChatBox(int l) {
		labels = new ArrayList<>();
		this.setBackground(UIUtils.Color_BACK);
	}
	RichLabel richLabel;
	/** 监听 */
	public InputBean isMonitor(final int x, final int y) {
		int History = 0;
		int hig = -this.deviation * 22;
		int i = 0;
		while (i < this.labels.size()) {
			richLabel = (RichLabel)this.labels.get(i);
			History = richLabel.getHeight();
			hig += History;
			if (hig >= 0 && hig >= y) {
				return richLabel.isMonitor(x, y - (hig - History));
			}
			else {
				++i;
			}
		}
		return null;
	}

	Component c;
	Graphics2D g2d;
	int History;
	int hig;
	int i;
	Composite instance = AlphaComposite.getInstance(3, this.Alpha);

	@Override
	public void paint(final Graphics g) {
		if (this.labels.size() != 0) {
			g2d = (Graphics2D)g.create();

			g2d.setComposite(instance);
			g2d.setColor(Color.BLACK);
			if (this.h == 0) {
				g2d.fillRoundRect(1, 1, this.getWidth() + 5, this.getHeight(), 0, 0);
				g2d.fillRoundRect(0, 0, this.getWidth() + 5, this.getHeight(), 0, 0);
			}
			else {
				g2d.fillRoundRect(1, 1, this.getWidth() + 5, this.h, 0, 0);
				g2d.fillRoundRect(0, 0, this.getWidth() + 5, this.h, 0, 0);
			}
			g2d.dispose();
			// 记录上一个窗体所用的高度
			History = 0;
			hig = -this.deviation * 22;
			g.translate(5, hig);
			i = 0;
			while (i < this.labels.size()) {
				c = (RichLabel)this.labels.get(i);
				g.translate(c.getX(), History);
				History = c.getHeight();
				hig += History;
				if (hig >= 0) {
					c.paint(g);
				}
				++i;
			}
		}
	}

	public void paintSSS1(final Graphics g) {
		if (this.labels.size() != 0) {
			g2d = (Graphics2D)g;
			g2d.setComposite((Composite)AlphaComposite.getInstance(3, 1.0f));
			g2d.setColor(Color.BLACK);
			ChatBox.imgZoomSx.draw3((Graphics)g2d);
			ChatBox.imgZoomSx.setMiddlew(this.getWidth() - 3 * ChatBox.imgZoomSx.getEdgew());
			ChatBox.imgZoomSx.setMiddleh(this.getHeight() - 3 * ChatBox.imgZoomSx.getEdgeh());
			int History = 0;
			int hig = 1 - this.deviation * 22;
			g2d.translate(8, hig);
			int i = 0;
			while (i < this.labels.size()) {
				c = (RichLabel)this.labels.get(i);
				g2d.translate(c.getX() - 5, History - 3);
				History = c.getHeight();
				hig += History;
				if (hig >= 0) {
					c.paint((Graphics)g2d);
					c.paint((Graphics)g2d);
					c.paint((Graphics)g2d);
					c.paint((Graphics)g2d);
				}
				++i;
			}
		}
	}

	TestSetupJpanel ttt;

	public void paintSSS(final Graphics g) {
		if (this.labels.size() != 0) {
			g2d = (Graphics2D)g;
			ttt = TestSetupJframe.getTestSetupJframe().getTestSetupJpanel();
			g2d.setComposite((Composite)AlphaComposite.getInstance(3, 1.0f));
			g2d.translate(0, -1);
			g2d.setColor(Color.BLACK);

				ChatBox.imgZoomS.draw((Graphics)g2d);
				ChatBox.imgZoomS.setMiddlew(this.getWidth() - 2 * ChatBox.imgZoomS.getEdgew());
				ChatBox.imgZoomS.setMiddleh(this.getHeight() - 2 * ChatBox.imgZoomS.getEdgeh());

			History = 0;
			hig = 1 - this.deviation * 22;
			g2d.translate(2, hig);
			i = 0;
			while (i < this.labels.size()) {
				c = (RichLabel)this.labels.get(i);
				g2d.translate(c.getX(), History);
				History = c.getHeight();
				hig += History;
				if (hig >= 0) {
					c.paint((Graphics)g2d);
					c.paint((Graphics)g2d);
					c.paint((Graphics)g2d);
					c.paint((Graphics)g2d);
				}
				++i;
			}
		}
	}
	public void addtext(String text, int size) {
		boolean is = this.deviation >= this.ScreenFull();
		this.addText(text, size, UIUtils.TEXT_FONT1);
		if (is) {
			this.update();
		}
		while(this.labels.size() > this.maxSize) {
			this.labels.remove(0);
		}

	}
	public void update() {
		if (this.ScreenFull() != 0 && flag) {
			while(this.deviation < this.ScreenFull()) {
				++this.deviation;
			}
		}

	}
	// * //系统200 信息201 世界202 当前203 队伍204 帮派205
	// * #T 系统* #X 信息* #J 世界* #Q 当前* #D 队伍* #P 帮派
	public void addtext(final RichLabel label, final int size) {
		if (this.labels.size() > 100) {
			this.AddAndSubtract(2);
		}
		if (label != null) {
			this.labels.add(label);
			removeLabels();
		}
		this.Formsize(size);
		if (this.ScreenFull() != 0) {
			while (this.deviation < this.ScreenFull()) {
				++this.deviation;
			}
		}
	}

	/**
	 * 自带的字体
	 *
	 * @param
	 * @param size
	 */
	public void addtext(final String text, final int size, final Font font) {
		if (this.labels.size() > 100) {
			this.AddAndSubtract(2);
		}
		this.addText(text, size, font);
		if (this.ScreenFull() != 0) {
			while (this.deviation < this.ScreenFull()) {
				++this.deviation;
			}
		}
	}

	/**
	 * 自定义字体
	 *
	 * @param chatText
	 * @param size
	 */
	public void addText(final String chatText, final int size) {
		final RichLabel label = new RichLabel(chatText, null);
		final Dimension d = label.computeSize(size);
		label.setSize(d);
		if (label != null) {
			this.labels.add(label);
			removeLabels();
		}
		this.add((Component)label);
		this.Formsize(size);
	}

	Pattern p = Pattern.compile("[一-龥]");
	Matcher m;

	public boolean checkcountname(final String countname) {
		 m = p.matcher((CharSequence)countname);
		return m.find();
	}

	private static boolean isNumber(final String str) {
		return StringUtils.isNotEmpty(str);
	}

	RichLabel label;
	Dimension dd;

	public RichLabel addText(final String chatText, final int size, Font font) {
		if (!this.checkcountname(chatText)) {
			font = UIUtils.TEXT_FONT2;
		}
		if (!isNumber(chatText)) {
			font = UIUtils.TXT_lianss;
		}
		label = new RichLabel(chatText, font);
		dd = label.computeSize(size);
		label.setSize(dd);
		this.labels.add(label);
//		//system.out.println("labels 长度 = " + labels.size());
//		removeLabels();
		this.Formsize(size);
		return label;
	}


	public void addTextx(final String chatText, final int size) {

		if (!this.checkcountname(chatText)) {
			font = UIUtils.TEXT_FONT2;
		}
		if (!isNumber(chatText)) {
			font = UIUtils.TXT_lianss;
		}
		label = new RichLabel(chatText, font);
		dd = label.computeSize(size);
		label.setSize(dd);
		this.labels.add(label);

		removeLabels();

		this.Formsize(size);
	}

	//清空系统消息，避免卡顿
	private void removeLabels() {
		if (labels.size() >= 16) {
			remove(labels.get(0));
			labels.remove(0);
		}
	}

	public RichLabel addText(final String chatText, final int size, final Font font, final String iconPath) {
		label = this.addText(chatText, size, font);
		return label;
	}

	/**TODO 移除式添加*/
	public void removeAddText(final String chatText, final int size, final Font font) {
		this.removeAll();
		label = (this.labels.size() != 0) ? ((RichLabel)this.labels.get(0)) : null;
		this.labels.clear();
		if (label == null) {
			label = new RichLabel(chatText, font);
			final Dimension d = label.computeSize(size);
			label.setSize(d);
		}
		else {
			label.setFont(font);
			label.setTextSize(chatText, size);
		}
		this.labels.add(label);
		removeLabels();
		this.Formsize(size);

	}
	/**
	 * 修改窗体大小
	 */
	public void Formsize(final int size) {
		int h = 6;
		int i = 0;
		while (i < this.labels.size()) {
			h = (int)((double)h + ((RichLabel)this.labels.get(i)).getSize().getHeight());
			++i;
		}
		this.setSize(size, h);
	}

	/**
	 * 点击上下清屏按钮 左(移位) 0 上 1下 2清屏 3禁言 4左 5调大聊天框 6调小聊天框大小 7透明度改变
	 *
	 * @param
	 */

	public void AddAndSubtract(final int type) {
		switch (type) {
			case 0: {
				if (this.ScreenFull() != 0 && this.deviation > 0) {
					--this.deviation;
					break;
				}
				else {
					break;
				}
			}
			case 1: {
				if (this.ScreenFull() != 0 && this.deviation < this.ScreenFull()) {
					++this.deviation;
					break;
				}
				else {
					break;
				}
			}
			case 2: {
				this.removemsg();
				this.deviation = 0;
				this.Formsize(280);
				break;
			}
			case 3: {
				this.Gag = !this.Gag;
				break;
			}
			case 4: {
				this.RemoveForms();
				break;
			}
			case 5: {
				if (this.h < ScrenceUntil.Screen_y - 300) {
					this.h += 20;
					break;
				}
				else {
					break;
				}
			}
			case 6: {
				if (this.h > 80) {
					this.h -= 20;
					break;
				}
				else {
					break;
				}
			}
			case 7: {
				if (this.display) {
					this.Alpha -= 0.2f;
					if (this.Alpha <= 0.0f) {
						this.Alpha = 0.6f;
					}
				}
			}
		}
	}

	/**
	 * 判断是否满屏
	 */
	int size;
	public int ScreenFull() {
		size = 0;
		i = 0;
		while (i < this.labels.size()) {
			size = (int)((double)size + ((RichLabel)this.labels.get(i)).getSize().getHeight());
			++i;
		}
		if (size - 22 >= this.h) {
			return (size - this.h) / 22 + 2;
		}
		else {
			return 0;
		}
	}

	/**
	 * 判断是否需要取消右窗体
	 */
	public void RemoveForms() {
		if (!display) {
			Alpha = 0.6f;
			w = 300;
			h = 200;
			FrameMessageChangeJpanel.chatbox1.removemsg();
			FrameMessageChangeJpanel.chatbox2.removemsg();
			ZhuFrame.getzhuframe().getChangeJpanel().setVisible(false);// 让面板显示为不可见
			TestMain.gameJframe.setSize(ScrenceUntil.Screen_x + 6,
					ScrenceUntil.Screen_y + 28);

			ZhuFrame.getZhuJpanel().outForms();
		} else {
			FrameMessageChangeJpanel.chatbox.removemsg();
			Alpha = 0.0f;
			w = 300;
			h = ScrenceUntil.Screen_y / 2 - 20;
			ZhuFrame.getzhuframe().getChangeJpanel().setVisible(true);// 让面板显示为可见
			TestMain.gameJframe.setSize(ScrenceUntil.Screen_x
					+ ScrenceUntil.ChatFram_X + 6, ScrenceUntil.Screen_y + 28);

			ZhuFrame.getZhuJpanel().intoForms();
		}
		display = !display;

	}
	/**清除对话记录*/

	public void removemsg() {
		this.removeAll();
		this.labels.clear();
	}

	public List<RichLabel> getLabels() {
		return this.labels;
	}

	public void setLabels(final List<RichLabel> labels) {
		this.labels = labels;
	}

	public int getDeviation() {
		return this.deviation;
	}

	public void setDeviation(final int deviation) {
		this.deviation = deviation;
	}

	public boolean isGag() {
		return this.Gag;
	}

	public void setGag(final boolean gag) {
		this.Gag = gag;
	}

	public float getAlpha() {
		return this.Alpha;
	}

	public void setAlpha(final float alpha) {
		this.Alpha = alpha;
	}

	public boolean isDisplay() {
		return this.display;
	}

	public void setDisplay(final boolean display) {
		this.display = display;
	}

	public int getW() {
		return this.w;
	}

	public void setW(final int w) {
		this.w = w;
	}

	public int getH() {
		return this.h;
	}

	public void setH(final int h) {
		this.h = h;
	}

	static {
//		提示文字添加图片外框
		if(MyIsif.getStyle().equals("水墨UI")) {

			imgZoom = CutButtonImage.cuts("inkImg/old/7/ss223.png", 14, 7, true);
			imgZoomS = CutButtonImage.cuts("inkImg/old/7/Z1007.png", 14, 7, true);
			imgZoomSx = CutButtonImage.cuts("inkImg/old/7/img_14.png", 6, 6, true);
		}else{

			imgZoom = CutButtonImage.cuts("inkImg/old/7/ss223.png", 14, 7, true);
			imgZoomS = CutButtonImage.cuts("inkImg/old/7/S145-2.png", 14, 7, true);
			imgZoomSx = CutButtonImage.cuts("inkImg/old/7/img_14.png", 6, 6, true);
		}
	}
}